package com.enqualcomm.kids.mvp.register;

import android.content.Context;
import android.content.res.TypedArray;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.activities.RegisterActivity;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.QueryChannelConfigParams;
import com.enqualcomm.kids.network.socket.response.QueryChannelConfigResult;
import com.enqualcomm.kids.view.BasePager;
import com.facebook.drawee.view.SimpleDraweeView;

@SuppressWarnings("ResourceType")
public class RegisterStep4Pager extends BasePager implements OnClickListener {

    private Button next_step_btn;
    private TextView register_username_tv;
    private TextView register_password_tv;
    private TextView describe_tv;
    SimpleDraweeView qr_image_view;
    AppDefault appDefault;
    Uri uri;

    public RegisterStep4Pager(Context context) {
        super(context);
    }


    @Override
    public View initView() {
        View root = View.inflate(ct, R.layout.pager_register_step4, null);
        next_step_btn = (Button) root.findViewById(R.id.next_step_btn);
        next_step_btn.setOnClickListener(this);
        appDefault = new AppDefault();
        register_username_tv = (TextView) root.findViewById(R.id.register_username_tv);
        register_password_tv = (TextView) root.findViewById(R.id.register_password_tv);
        qr_image_view = (SimpleDraweeView) root.findViewById(R.id.qr_image_view);
        if (appDefault.getChannelConfig() != null) {
            if (!TextUtils.isEmpty(appDefault.getChannelConfig().twocodepic)) {
                uri = Uri.parse(appDefault.getChannelConfig().twocodepic);
                qr_image_view.setImageURI(uri);

            }
        } else {
            QueryChannelConfigParams params = new QueryChannelConfigParams();
            new NetworkModel().loadDataFromServer(new SocketRequest<QueryChannelConfigResult>(params, new NetworkListener<QueryChannelConfigResult>() {
                @Override
                public void onError(VolleyError error) {

                }

                @Override
                public void onSuccess(QueryChannelConfigResult response) {
                    if (response.code == 0) {
                        new AppDefault().setChannelConfig(response);
                        if (!TextUtils.isEmpty(response.result.twocodepic)) {
                            uri = Uri.parse(response.result.twocodepic);
                            qr_image_view.setImageURI(uri);
                        }
                    }
                }
            }));
        }
        describe_tv = (TextView) root.findViewById(R.id.describe_tv);

        String text = ct.getString(R.string.wx_gg_decribe_tv_1) + "<font color=\"#86c610\">" + ct.getString(R.string.wx_gg_decribe_tv_2) + "</font>" + ct.getString(R.string.wx_gg_decribe_tv_3);
        describe_tv.setText(Html.fromHtml(text));
        return root;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_step_btn:
                ((RegisterActivity) ct).nextPage(4, next_step_btn.getText().toString());
                break;

            default:
                break;
        }
    }

    public void setData(String btnName, String username, String password) {
        TypedArray typedArray = ct.obtainStyledAttributes(new int[]{R.attr.bg_add_watch_btn_big_1, R.attr.bg_add_watch_btn_big_2});

        if (RegisterActivity.ADD_ACT.equals(btnName)) {
//            next_step_btn.setBackgroundDrawable(typedArray.getDrawable(0));
            next_step_btn.setText(RegisterActivity.ADD_ACT);
        } else {
//            next_step_btn.setBackgroundDrawable(typedArray.getDrawable(1));
            next_step_btn.setText(RegisterActivity.MAIN_ACT);
        }

        register_username_tv.setText(ct.getString(R.string.your_account) + username);
        register_password_tv.setText(ct.getString(R.string.your_password) + password);
    }

}
